package demo2;

class Alg <T extends Comparable<T>> {
    public T retArrMax(T[] arr) {
        T max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            //error why? -> 因为 T 是应用类型，不能最终会被擦除为 Object
            //这而 Object 没有实现 Comparable 所以无法比较
//            if(max < arr[i]) {
//                max = arr[i];
//            }
            //正确的写法
            //限制泛型（T）的范围 用 extends
            //它所继承的类必须要能实现 Comparable
            if(max.compareTo(arr[i]) < 0) {
                max = arr[i];
            }
        }
        return max;
    }
}

class Alg2  {
    public static <T extends Comparable<T>> T retArrMax(T[] arr) {
        T max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if(max.compareTo(arr[i]) < 0) {
                max = arr[i];
            }
        }
        return max;
    }
    public <T extends Person> void k(T q, T q2) {
        q.compareTo(q2);
    }

}

public class TestDemo2 {
    public static void main(String[] args) {
        Integer[] integers3 = {1,2,33};
        System.out.println(Alg2.<Integer>retArrMax(integers3));
        System.out.println("--------------");
        Alg2 alg2 = new Alg2();
        Integer[] integers2 = {12,2,3};
        //alg2.retArrMax(integers2) or alg2.<Integer>retArrMax(integers2)
        System.out.println(alg2.<Integer>retArrMax(integers2));
        System.out.println("--------------");
        Alg<Integer> alg1 = new Alg<>();
        Integer[] integers = {1,2,3};
        System.out.println(alg1.retArrMax(integers));
    }
    public static void main2(String[] args) {
        //Student extends Person
        Test<Person> p1 = new Test<>();
        Test<Student> p2 = new Test<>();
//        Test<Integer> p3 = new Test<>();//error
    }
    public static void main1(String[] args) {
        MyArray<String> myArray = new MyArray<>();
        MyArray<Integer> myArray1 = new MyArray<>();
        myArray.setValue(1,"ddw");
        System.out.println(myArray.getValue(1));
        myArray1.setValue(2, 12);
        System.out.println(myArray.getValue(2));
    }
}

class MyArray <T> {
    public Object[] arr = new Object[10];
    public void setValue(int pos, T n) {
        arr[pos] = n;
    }
    public T getValue(int pos) {
        return (T) arr[pos];
    }
}


class Person implements Comparable<Person> {

    @Override
    public int compareTo(Person o) {
        return 0;
    }
}

class Student extends Person {
    public int compareTo(Person o) {
        return 0;
    }
}

class Test<T extends Person> {

}